var express = require('express');
var router = express.Router();
const UsersApi = require("../api/users");


// 登录
router.get('/login', async function (req, res, next) {
    const { userName, userPassword } = req.query;
    let data = await UsersApi.login({ userName, userPassword });
    res.send(data);
});
/* GET home page. */
router.get('/login', async function (req, res, next) {
    const { userName, userPassword } = req.query;
    console.log(userName, userPassword);
    const data = await UsersApi.login({ userName, userPassword });
    res.send(data);
})

router.get('/', async function (req, res, next) {
    const { page, limit } = req.query;
    const result = await UsersApi.get({ page, limit });
    res.send(
        {
            "code": 0,
            "msg": "",
            "count": result.total,
            "data": result.rows,
        }
    );
});

router.get('/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const data = await UsersApi.getOne(_id);
    res.send(data);
});

router.put('/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const student = req.body;
    const data = await UsersApi.updata({ _id }, student);
    res.send(data);
})
router.post('/', async function (req, res, next) {
    const user = req.body;
    const data = await UsersApi.add(user);
    res.send(data);
})

router.delete('/', async function (req, res, next) {
    const { _id } = req.body;
    const data = await UsersApi.del(_id);
    res.send(data);
})
router.post('/reg', async function (req, res, next) {
    const { userName, userPassword } = req.body;
    console.log(userName, userPassword);
    const data = await UsersApi.reg({ userName, userPassword });
    res.send(data);
})



// 注册
router.post('/reg', async function (req, res, next) {
    const { userName, userPassword } = req.body;
    let data = await UsersApi.reg({ userName, userPassword });
    // 数据库操作

    res.send(data);
});

module.exports = router;